<?php

$i = 0;
$coords = "";

$action = $_POST["action"];
if ($action == "1")
{
	echo generate_list($_POST["resultsFrom"]);
}
else if ($action == "2")
{
	echo generate_hidden_elements($_POST["resultsFrom"]);
}
else if ($action == "3")
{
	reserve_pickup($_POST["id"]);
}
function reserve_pickup($id)
{
	include ("dbconfig.php");
	
	$id =  mysql_real_escape_string($id);
	
	$query = <<<END
	UPDATE `hestia`.`pickups` SET `reserved`='reserved' WHERE `idpickup`='$id';
END;
	mysql_query ( $query, $connection );
}
function generate_hidden_elements($resultsFrom){
	include ("dbconfig.php");

	$query = <<<END
		SELECT *
		FROM  pickups
		LIMIT $resultsFrom, 10; 
END;


	
	$result = mysql_query ( $query, $connection );
	$size = mysql_num_rows($result); 
	$output = <<<END
		<p id="numOfResults">$size</p>
END;
	$i = 0;
	while ( $row = mysql_fetch_array ( $result ) ) {
		$id = $row[0];
		$name = $row[1]." ".$row[2];
		$phone_number = "(".$row[3].")".$row[4]."-".$row[5];
		$address_line_one = $row[6];
		$address_line_two = $row[8].", ".$row[9];
		$email = $row[10];
		$times = $row[11];
		$tree_info = $row[12];
		$add_info = $row[14];
		$status = $row[15];
		$lat = $row[16];
		$lng = $row[17];
		
		$statustext = "";
		if ($status == "complete") {
                break;
        }
        if ($status == "open") {
                $statustext = <<<END
                    <input id="saveForm" type="submit" onclick="reserve_pickup($id)" value="Reserve Pickup" />
END;
        } else {
                $statustext = "Reserved";
        }
		
		$output .= <<<END
		<div id = "lat$i">$lat</div>
		<div id = "lng$i">$lng</div>
		<div id = "contact$i">
			<div>$name</div>
			<div>$address_line_one</div>
			<div>$address_line_two</div>
			<div>$phone_number</div>
			<div>$email</div>
			<p>$times</p>
			<p>$tree_info</p>
			<p>$add_info</p>
			<div>$statustext</div>
		</div>
END;
		$i++;
	}
	return $output;
}
function generate_list($resultsFrom)
{
	include ("dbconfig.php");
$query = <<<END
	SELECT street_address, second_line, city, zipcode 
	FROM  pickups
	LIMIT $resultsFrom, 10; 
END;

$output = '<select size="10">';	

	
$result = mysql_query ( $query, $connection );
	$i = 0;
	while ( $row = mysql_fetch_array ( $result ) ) {
		$address = $row[0]." ".$row[1]." ".$row[2]; 
		
		$output .= <<<END
		<option value="$address" onclick="openWindow($i)">$address</option>
END;
		$i++;
	}
	$output .= "</select>";
	return $output;
	
}
?>